﻿using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Project.Apps.DeptApp.Dto;
using Project.Entities;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Project.Apps.DeptApp
{
    public class DeptAppService : ApplicationService, IDeptAppService
    {
        private readonly IRepository<Dept> _reposDept;
        private readonly IRepository<Person> _reposPerson;
        public DeptAppService(IRepository<Dept> reposDept, IRepository<Person> reposPerson)
        {
            _reposDept = reposDept;
            _reposPerson = reposPerson;
        }
        public async Task<List<Person>> GetPersonsByDeptId(EntityDto<long> input)
        {
            return await _reposPerson.GetAllListAsync(p => p.DeptId.Value == input.Id);
        }

        public async Task<int> InsertOrUpdateDept(DeptDto input)
        {
            Dept dept = null;
            dept = await _reposDept.FirstOrDefaultAsync(x => x.Id == input.Id);
            if (dept == null)
            {
                dept = new Dept();
            }
            dept.Id = input.Id;
            dept.Name = input.Name;
            dept.PhoneNumber = input.PhoneNumber;
            dept.TenantId = input.TenantId;
            dept.Code = input.Code;
            dept.Comment = input.Comment;
            return await _reposDept.InsertOrUpdateAndGetIdAsync(dept);
        }
    }
}
